<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户id">
          <el-input v-model="form.mer_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="商户名称">
          <el-input v-model="form.mer_name" placeholder="商户名称" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="110"
          />
          <el-table-column
            prop="name"
            label="商户名称"
            align="center"
            width="130"
          />
          <el-table-column prop="nickname" label="商户昵称" align="center" />
          <el-table-column prop="category" label="分类" align="center" />
          <el-table-column prop="sub_category" label="子分类" align="center" />
          <el-table-column label="KYC" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_kyc ? 'success' : 'danger'">{{
                scope.row.is_kyc ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="冻结" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.is_frozen ? 'success' : 'danger'">{{
                scope.row.is_frozen ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="法币钱包" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.open_currency ? 'success' : 'danger'">{{
                scope.row.open_currency ? "已开通" : "未开通"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="法币付款" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.open_currency_payment ? 'success' : 'danger'"
                >{{
                  scope.row.open_currency_payment ? "已开通" : "未开通"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="法币充值" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.open_currency_recharge ? 'success' : 'danger'"
                >{{
                  scope.row.open_currency_recharge ? "已开通" : "未开通"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <!-- <el-table-column label="法币兑换USDM" align="center">
                    <template #default="scope">
                            <el-tag :type="scope.row.open_currency_swap ? 'success' : 'danger'">{{ scope.row.open_currency_swap ? "已开通" : "未开通" }}</el-tag>
                        </template>
                    </el-table-column> -->
          <el-table-column label="法币换汇" align="center">
            <template #default="scope">
              <el-tag
                :type="scope.row.open_currency_exchange ? 'success' : 'danger'"
                >{{
                  scope.row.open_currency_exchange ? "已开通" : "未开通"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="创建时间"
            :formatter="formatterTime"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'frozen')"
                >冻结</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <AddDialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :addDialogVisible="addDialogVisible"
      :addDialogForm="addDialogForm"
      :btnLoading="btnLoading"
    />
    <EditDialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :editDialogVisible="editDialogVisible"
      :editDialogForm="editDialogForm"
      :btnLoading="btnLoading"
    />
    <el-dialog v-model="acqInfoVisible" title="商户收单通道接入信息">
      <el-form>
        <el-form-item label="商户密钥：" label-width="150px">{{
          acqInfoData.secret_key
        }}</el-form-item>
        <el-form-item label="回调接口URL：" label-width="150px">{{
          acqInfoData.callback_url
        }}</el-form-item>
        <el-form-item label="退款接口URL：" label-width="150px">{{
          acqInfoData.refund_url
        }}</el-form-item>
        <el-form-item label="成功跳转URL：" label-width="150px">{{
          acqInfoData.page_url
        }}</el-form-item>
        <el-form-item label="失败跳转URL：" label-width="150px">{{
          acqInfoData.failed_url
        }}</el-form-item>
        <el-form-item label="保证金冻结比例：" label-width="150px"
          >{{ acqInfoData.freeze_ratio * 100 }}%</el-form-item
        >
        <el-form-item label="保证金冻结天数：" label-width="150px"
          >{{ acqInfoData.freeze_days }}天</el-form-item
        >
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  MerListApi,
  payMerAddApi,
  payMerUpdateApi,
  payMerFrozenApi,
  merchantAcquiringInfoApi,
} from "@/api/ExAssistant/merchant";
import AddDialogForm from "./components/merechantAdd.vue";
import EditDialogForm from "./components/merechantEdit.vue";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const loading = ref(false);
const tableData = ref([]);
const addDialogVisible = ref(false);
const editDialogVisible = ref(false);
const acqInfoVisible = ref(false);
const btnLoading = ref(false);
const acqInfoData = ref({});
const btnType = ref(null);
// 查询form
const formData = {
  mer_id: "",
  name: "",
};
const form = reactive({ ...formData });
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 新增商户参数
const addDialogFormData = {
  name: "",
  nickname: "",
  merchant_id: "",
  channel_id: "",
  currency: "",
  email: "",
  callback_url: "",
  page_url: "",
  failed_url: "",
  rate: "",
  min_amount: "",
  max_amount: "",
  category: "",
  sub_category: "",
};
const addDialogForm = reactive({ ...addDialogFormData });
// 编辑商户参数
const editDialogFormData = {
  name: "",
  nickname: "",
  merchant_id: "",
  callback_url: "",
  page_url: "",
  failed_url: "",
  category: "",
  sub_category: "",
};
const editDialogForm = reactive({ ...editDialogFormData });
// 查询参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    mer_id: form.mer_id,
    mer_name: form.mer_name,
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await MerListApi(param());
    globalStore.setMerchant();
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  getTbaleData();
};
// 新增
const onAdd = async () => {
  addDialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  editDialogVisible.value = false;
  addDialogVisible.value = false;
  btnType.value = null;
  Object.assign(addDialogForm, { ...addDialogFormData });
  Object.assign(editDialogForm, { ...editDialogFormData });
};

// 取消
const cancel = () => {
  editDialogVisible.value = false;
  addDialogVisible.value = false;
  btnType.value = null;
  Object.assign(addDialogForm, { ...addDialogFormData });
  Object.assign(editDialogForm, { ...editDialogFormData });
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await payMerAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
      addDialogVisible.value = false;
    }
    if (btnType.value == "edit") {
      //            console.log(param);
      const res = await payMerUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
      editDialogVisible.value = false;
    }
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    addDialogVisible.value = false;
    editDialogVisible.value = false;
  }
};
// 查看商户收单密钥等信息
const handleAcquiringInfo = async (merchant_id) => {
  const info = await merchantAcquiringInfoApi({ merchant_id: merchant_id });
  acqInfoVisible.value = true;
  acqInfoData.value = info.data;
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(editDialogForm, { ...row });
    editDialogVisible.value = true;
  }
  // 冻结
  if (type === "frozen") {
    ElMessageBox.confirm("您是否确认冻结此商户?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await payMerFrozenApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// //上传图标
const handleUploader = (res, type) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    dialogForm.src = res.data.pic_url || "";
    dialogForm.icon = res.data.name || "";
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
